package Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Entity.Room;
import Entity.RoomHelp;
import Service.RoomService;

public class CheckRoomServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public CheckRoomServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//response.setContentType("text/html");
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String op=request.getParameter("op");
		String mid=request.getParameter("mid");
		String mname=request.getParameter("mname");
		request.setAttribute("mid", mid);
		request.setAttribute("mname", mname);
		if(op!=""&&op!=null&&op.equals("query")){
			String week=request.getParameter("week");
			String day=request.getParameter("day");
			String section=request.getParameter("section");
			int w=-1;
			if(week!=""&&week!=null)
			w=Integer.parseInt(week);
			int d=-1;
			if(day!=""&&day!=null)
			d=Integer.parseInt(day);
			int s=-1;
			if(section!=""&&section!=null)
			s=Integer.parseInt(section);
			RoomService ser=new RoomService();
			List<RoomHelp> list=null;
			if(ser!=null)
			list=ser.findRoomByTime(w, d, s);
			request.setAttribute("list",list);
			request.setAttribute("week", week);
			request.setAttribute("day", day);
			request.setAttribute("section", section);
	    	request.getRequestDispatcher("/WEB-INF/page/CheckRoom.jsp").forward(request,response);
	    	return;
		}
		else if(op!=""&&op!=null&&op.equals("add1")){
			request.getRequestDispatcher("/WEB-INF/page/AddRoom.jsp").forward(request,response);
		}
		else if(op!=""&&op!=null&&op.equals("add")){
			String roomid=request.getParameter("roomid");
			String total=request.getParameter("total");
			String type=request.getParameter("type");
			int tal=0;
			int pe=0;
			if(total!=null&&total!="") tal=Integer.parseInt(total);
			if(type!=null&&type!="") pe=Integer.parseInt(type);
			if(roomid!=null&&roomid!=""){
				RoomService ser=new RoomService();
				Room r=new Room();
				r.setRoomid(roomid);r.setRoomtype(pe);r.setTotal(tal);
				Room tmp=ser.findByRoomid(roomid);
				if(tmp==null)
					ser.addRoom(r);
			}
		}
		else if(op!=""&&op!=null&&op.equals("quit")){
			request.getRequestDispatcher("/WEB-INF/page/Login.jsp").forward(request,response);
			return;
		}
		else if(op!=""&&op!=null&&op.equals("del")){
			String roomid=request.getParameter("roomid");
			//判断：如果有学生选了该教室，就不能直接删除，交给前端判断了，如果有学生预约该教室，则不能删除。
			RoomService ser=new RoomService();
			ser.deleteRoom(roomid);
		}
		else if(op!=""&&op!=null&&op.equals("j")){
			request.setAttribute("id", "10101");
			request.getRequestDispatcher("/WEB-INF/page/CheckRoom.jsp").forward(request,response);
			return;
		}
			RoomService ser=new RoomService();
			List<RoomHelp> list=null;
			if(ser!=null)
			list=ser.findRoomByTime(-1, -1, -1);//即查询全部
			request.setAttribute("list",list);
	    	request.getRequestDispatcher("/WEB-INF/page/CheckRoom.jsp").forward(request,response);
		
		
		
		
		
		
		
		
		
		

		
		
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
